package com.dxb.yomall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.dxb.yomall.product.entity.ProductAttrValueEntity;
import com.dxb.yomall.product.service.ProductAttrValueService;
import com.dxb.yomall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dxb.yomall.product.entity.AttrEntity;
import com.dxb.yomall.product.service.AttrService;
import com.dxb.common.utils.PageUtils;
import com.dxb.common.utils.R;



/**
 * 
 *
 * @author daixinbao
 * @email 1065808217@qq.com
 * @date 2021-01-05 22:26:07
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    /**
     * 修改SPU规格属性
     * @param spuId
     * @param entities
     * @return
     */
     @PostMapping("/update/{spuId}")
     public R updateSpuAttr(@PathVariable("spuId") Long spuId,
     @RequestBody List<ProductAttrValueEntity> entities){
     productAttrValueService.updateSpuAttr(spuId, entities);
     return R.ok();
     }
    /**
     * 获取SPU规格属性
     * @param spuId
     * @return
     */
     @GetMapping("/base/listforspu/{spuId}")
     public R listBaseAttrForSpu(@PathVariable("spuId") Long spuId) {
     List<ProductAttrValueEntity> entities =
     productAttrValueService.listBaseAttrForSpu(spuId);
     return R.ok().put("data", entities);
     }
    /**
     * 列表
     */
    @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 获取规格参数
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrVO attrVO = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrVO);
    }
    /**
     * 保存规格参数
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrVO attrVO){
        attrService.saveAttr(attrVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attrVO){
        attrService.updateAttr(attrVO);
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }
    /**
     * 查询规格参数和销售属性列表
     * @param params 分页信息(page,limit)和查询关键字key
     * @param catelogId 三级分类ID
     * @param attrType 属性类型
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R listAttr(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId,
                      @PathVariable("attrType") String attrType) {
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);
        return R.ok().put("page", page);
    }
}
